package com.ruoyi.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

/**
 * @author: liufei
 * @Desc:
 * @create: 2024-11-07 13:18
 **/
@Configuration
public class RabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 保证数据的完整性
     */
    public void init(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean ack, String cause) {
                if (ack){
                    //消息成功到达交换机
                    System.out.println("消息成功到达交换机");
                }else {
                    //消息没有到达交换机
                    System.out.println("消息没有到达交换机");
                }
            }
        });

        rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
            @Override
            public void returnedMessage(Message message, int i, String s, String s1, String s2) {
                System.out.println("消息没有到达队列");
                System.out.println("消息:"+message);
                System.out.println("状态:"+i);
                System.out.println("交换机:"+s);
                System.out.println("路由:"+s1);
            }
        });
    }


}

